//
//  PlaygroundController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-25.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "PlaygroundController.h"
#import "UIView+WhenTappedBlocks.h"
#import "SimpleMatchCell.h"
#import "Playground.h"
#import "PlaygroundCategory.h"
#import "Place.h"
#import "PlaceService.h"
#import "MatchByPlaygroundViewController.h"
#import "AppDelegate.h"

@interface PlaygroundController ()

@property (nonatomic, strong) NSArray* playgroundCategories;
@property (nonatomic, strong) NSArray*/*Place*/ places;
@property (nonatomic, strong) NSArray*/*SubPlace*/ subplaces;
@property (nonatomic, strong) Place* curPlace; //当前选中的场馆

@property (nonatomic, strong)IBOutlet UITableView *listView;

@property (nonatomic, strong) IBOutlet UIView *shadeView;
@property (nonatomic, strong) IBOutlet UIView* activityView;
@property (nonatomic, strong) IBOutlet UITableView *infoListView;
@property (nonatomic, strong)IBOutlet UIImageView *iconView;
@property (nonatomic, strong)IBOutlet UILabel *titleLabel;
@property (nonatomic, strong)IBOutlet UILabel *subTitleLabel;
@property (nonatomic, getter = isShowing) BOOL show;

@end

@implementation PlaygroundController
@synthesize places,subplaces,curPlace;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.playgroundCategories = [Playground defaultPlaygroundCategories];
    
    if ([theApp isAppReachable]) {
        [self updateMainDataAndView];
    }
    
    [self setupActivityView];
}

- (void)updateMainDataAndView
{
    [self.HUD show:YES];
    
    [[[PlaceService alloc] init] getAllPlaces:^(id result, NSError *error) {
        [self.HUD hide:YES];
        
        if (nil == error) {
            self.places = result;
            [self.listView reloadData];
            NSLog(@"result is: %@",[AppUtils replaceUnicodeDic:result]);
        }else{
            //TODO
        }
    }];
}


#pragma mark - TableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.listView){
        return self.places.count;
    }else if(tableView == self.infoListView){
        return self.subplaces.count;
    }
    return 0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *tableViewCell = nil;
    
    if (tableView == self.listView) {
        SimpleMatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"playgroundCategoryCell" forIndexPath:indexPath];
        
        Place* place = self.places[indexPath.row];
        cell.titleLabel.text = place.name;
        cell.subTitleLabel.text = place.address;
        cell.iconView.image = place.icon;
        
        //
        tableViewCell = cell;
        
    }else if(tableView == self.infoListView){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pgDetailCell" forIndexPath:indexPath];
        
        SubPlace* subplace = self.subplaces[indexPath.row];
        cell.textLabel.text = subplace.name;
        //
        tableViewCell = cell;
    }
    
    return tableViewCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.listView) {
        Place* place = self.places[indexPath.row];
        curPlace = place;
        if(place.hasSubLocation){
            self.titleLabel.text = place.name;
            self.subTitleLabel.text = place.address;
            self.iconView.image = place.icon;
            //
            [[[PlaceService alloc] init] getAllSubPlaces:place.id completion:^(id result, NSError* error){
                SubGame *all = [[SubGame alloc]init];
                all.id = @"";
                all.name = NSLocalizedString(@"所有", nil);
                //
                NSMutableArray* sg = [[NSMutableArray alloc] initWithObjects:all,nil];
                [sg addObjectsFromArray:result];
                //
                self.subplaces = [NSArray arrayWithArray: sg];
                [self.infoListView reloadData];
            }];
            //
            [self showActivityView];
        }else{
            MatchByPlaygroundViewController *matchCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"matchByPCtrl"];
            //
            [matchCtrl initWithPlace:place andSubPlaceId:@"" andDate:[AppUtils getCurYMDDateStrInYogRange]];
            [self.navigationController pushViewController:matchCtrl animated:YES];
            [self hideActivityView];
        }
        
    }else if(tableView == self.infoListView){
        MatchByPlaygroundViewController *matchCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"matchByPCtrl"];
        SubPlace* subplace = self.subplaces[indexPath.row];
        //
        [matchCtrl initWithPlace:curPlace andSubPlaceId: subplace.id andDate:[AppUtils getCurYMDDateStrInYogRange]];
        [self.navigationController pushViewController:matchCtrl animated:YES];
        [self hideActivityView];
    }
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - ActivityView
-(void)setupActivityView
{
    
    self.activityView.frame = CGRectMake(0, ScreenHeight, self.activityView.frame.size.width, self.activityView.frame.size.height);
    self.shadeView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    
    
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self.shadeView];
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self.activityView];
    
    
    self.shadeView.hidden = YES;
    [self.shadeView whenTapped:^{
        [self hideActivityView];
    }];
    
}
-(void)showActivityView{
    if (self.isShowing) {
        return;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.shadeView.hidden = NO;
        self.activityView.frame = CGRectMake(0,
                                             ScreenHeight- self.activityView.frame.size.height,
                                             self.activityView.frame.size.width,
                                             self.activityView.frame.size.height);
        
        self.shadeView.alpha = .2;
    } completion:^(BOOL finished) {
        
        self.show = YES;
    }];
}

-(void)hideActivityView{
    if (!self.isShowing) {
        return;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.shadeView.alpha = 0;
        self.shadeView.alpha = 0.0;
        self.shadeView.hidden = YES;
        self.activityView.frame = CGRectMake(0, ScreenHeight, self.activityView.frame.size.width, self.activityView.frame.size.height);
        [self.infoListView reloadData];
    } completion:^(BOOL finished) {
        self.show = NO;
    }];
}
-(IBAction)cancelAction:(id)sender
{
    [self hideActivityView];
}





@end
